/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xcdh.learn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xcdh.commons.controller.BaseController;
import com.xcdh.domain.vo.PageVO;
import com.xcdh.domain.vo.R;
import com.xcdh.learn.commons.Condition;
import com.xcdh.learn.commons.Query;
import com.xcdh.learn.entity.Site;
import com.xcdh.learn.service.ISiteService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 个人收藏的网址 控制器
 *
 *
 * @since 2022-05-30
 */
@RestController
@AllArgsConstructor
@RequestMapping("/site")
public class SiteController extends BaseController<Site , ISiteService> {

	private ISiteService siteService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	public R detail(Site site) {
		Site detail = siteService.getOne(Condition.getQueryWrapper(site));
		return R.okData(detail);
	}

	/**
	 * 分页 个人收藏的网址
	 */
	@GetMapping("/list")
	public R<PageVO<Site>> list(Site site, Query query) {
		QueryWrapper<Site> wrapper = new QueryWrapper<>() ;
		Map<String,Object> params = new HashMap<>() ;
		params.put("site_name" , site.getSiteName()) ;
		params.put("owner" , site.getOwner()) ;
		params.put("create_time" , site.getCreateTime()) ;
		params.put("id" , site.getId()) ;
		wrapper.allEq(params , false) ;
		wrapper.orderByAsc("`sort`") ;
		IPage<Site> pages = siteService.page(Condition.getPage(query), wrapper);
		return R.page(pages);
	}

	/**
	 * 自定义分页 个人收藏的网址
	 */
	@GetMapping("/page")
	public R<PageVO<Site>> page(Site site, Query query) {
		IPage<Site> pages = siteService.selectSitePage(Condition.getPage(query), site);
		return R.page(pages);
	}


	/**
	 * 新增或修改 个人收藏的网址
	 */
	@PostMapping("/submit")
	public R submit( @RequestBody Site site) {
		return R.result(siteService.saveOrUpdate(site));
	}


	@PostMapping("/remove")
	public R remove(String []ids){
		return R.result(siteService.removeByIds(Arrays.asList(ids)) );
	}

}
